{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# wedding_optimal_chart_sat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/examples/wedding_optimal_chart_sat.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/python/wedding_optimal_chart_sat.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "Finding an optimal wedding seating chart.\n",
    "\n",
    "From\n",
    "Meghan L. Bellows and J. D. Luc Peterson\n",
    "\"Finding an optimal seating chart for a wedding\"\n",
    "http://www.improbable.com/news/2012/Optimal-seating-chart.pdf\n",
    "http://www.improbable.com/2012/02/12/finding-an-optimal-seating-chart-for-a-wedding\n",
    "\n",
    "Every year, millions of brides (not to mention their mothers, future\n",
    "mothers-in-law, and occasionally grooms) struggle with one of the\n",
    "most daunting tasks during the wedding-planning process: the\n",
    "seating chart. The guest responses are in, banquet hall is booked,\n",
    "menu choices have been made. You think the hard parts are over,\n",
    "but you have yet to embark upon the biggest headache of them all.\n",
    "In order to make this process easier, we present a mathematical\n",
    "formulation that models the seating chart problem. This model can\n",
    "be solved to find the optimal arrangement of guests at tables.\n",
    "At the very least, it can provide a starting point and hopefully\n",
    "minimize stress and arguments.\n",
    "\n",
    "Adapted from\n",
    "https://github.com/google/or-tools/blob/master/examples/csharp/wedding_optimal_chart.cs\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "from typing import Sequence\n",
    "from ortools.sat.python import cp_model\n",
    "\n",
    "\n",
    "class WeddingChartPrinter(cp_model.CpSolverSolutionCallback):\n",
    "    \"\"\"Print intermediate solutions.\"\"\"\n",
    "\n",
    "    def __init__(self, seats, names, num_tables, num_guests):\n",
    "        cp_model.CpSolverSolutionCallback.__init__(self)\n",
    "        self.__solution_count = 0\n",
    "        self.__start_time = time.time()\n",
    "        self.__seats = seats\n",
    "        self.__names = names\n",
    "        self.__num_tables = num_tables\n",
    "        self.__num_guests = num_guests\n",
    "\n",
    "    def on_solution_callback(self):\n",
    "        current_time = time.time()\n",
    "        objective = self.objective_value\n",
    "        print(\n",
    "            \"Solution %i, time = %f s, objective = %i\"\n",
    "            % (self.__solution_count, current_time - self.__start_time, objective)\n",
    "        )\n",
    "        self.__solution_count += 1\n",
    "\n",
    "        for t in range(self.__num_tables):\n",
    "            print(\"Table %d: \" % t)\n",
    "            for g in range(self.__num_guests):\n",
    "                if self.value(self.__seats[(t, g)]):\n",
    "                    print(\"  \" + self.__names[g])\n",
    "\n",
    "    def num_solutions(self) -> int:\n",
    "        return self.__solution_count\n",
    "\n",
    "\n",
    "def build_data():\n",
    "    \"\"\"Build the data model.\"\"\"\n",
    "    # Easy problem (from the paper)\n",
    "    # num_tables = 2\n",
    "    # table_capacity = 10\n",
    "    # min_known_neighbors = 1\n",
    "\n",
    "    # Slightly harder problem (also from the paper)\n",
    "    num_tables = 5\n",
    "    table_capacity = 4\n",
    "    min_known_neighbors = 1\n",
    "\n",
    "    # Connection matrix: who knows who, and how strong\n",
    "    # is the relation\n",
    "    connections = [\n",
    "        [1, 50, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
    "        [50, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
    "        [1, 1, 1, 50, 1, 1, 1, 1, 10, 0, 0, 0, 0, 0, 0, 0, 0],\n",
    "        [1, 1, 50, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
    "        [1, 1, 1, 1, 1, 50, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
    "        [1, 1, 1, 1, 50, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
    "        [1, 1, 1, 1, 1, 1, 1, 50, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
    "        [1, 1, 1, 1, 1, 1, 50, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
    "        [1, 1, 10, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
    "        [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 50, 1, 1, 1, 1, 1, 1],\n",
    "        [0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 1, 1, 1, 1, 1, 1, 1],\n",
    "        [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],\n",
    "        [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],\n",
    "        [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],\n",
    "        [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],\n",
    "        [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],\n",
    "        [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],\n",
    "    ]\n",
    "\n",
    "    # Names of the guests. B: Bride side, G: Groom side\n",
    "    names = [\n",
    "        \"Deb (B)\",\n",
    "        \"John (B)\",\n",
    "        \"Martha (B)\",\n",
    "        \"Travis (B)\",\n",
    "        \"Allan (B)\",\n",
    "        \"Lois (B)\",\n",
    "        \"Jayne (B)\",\n",
    "        \"Brad (B)\",\n",
    "        \"Abby (B)\",\n",
    "        \"Mary Helen (G)\",\n",
    "        \"Lee (G)\",\n",
    "        \"Annika (G)\",\n",
    "        \"Carl (G)\",\n",
    "        \"Colin (G)\",\n",
    "        \"Shirley (G)\",\n",
    "        \"DeAnn (G)\",\n",
    "        \"Lori (G)\",\n",
    "    ]\n",
    "    return num_tables, table_capacity, min_known_neighbors, connections, names\n",
    "\n",
    "\n",
    "def solve_with_discrete_model() -> None:\n",
    "    \"\"\"Discrete approach.\"\"\"\n",
    "    num_tables, table_capacity, min_known_neighbors, connections, names = build_data()\n",
    "\n",
    "    num_guests = len(connections)\n",
    "\n",
    "    all_tables = range(num_tables)\n",
    "    all_guests = range(num_guests)\n",
    "\n",
    "    # Create the cp model.\n",
    "    model = cp_model.CpModel()\n",
    "\n",
    "    #\n",
    "    # Decision variables\n",
    "    #\n",
    "    seats = {}\n",
    "    for t in all_tables:\n",
    "        for g in all_guests:\n",
    "            seats[(t, g)] = model.new_bool_var(\"guest %i seats on table %i\" % (g, t))\n",
    "\n",
    "    colocated = {}\n",
    "    for g1 in range(num_guests - 1):\n",
    "        for g2 in range(g1 + 1, num_guests):\n",
    "            colocated[(g1, g2)] = model.new_bool_var(\n",
    "                \"guest %i seats with guest %i\" % (g1, g2)\n",
    "            )\n",
    "\n",
    "    same_table = {}\n",
    "    for g1 in range(num_guests - 1):\n",
    "        for g2 in range(g1 + 1, num_guests):\n",
    "            for t in all_tables:\n",
    "                same_table[(g1, g2, t)] = model.new_bool_var(\n",
    "                    \"guest %i seats with guest %i on table %i\" % (g1, g2, t)\n",
    "                )\n",
    "\n",
    "    # Objective\n",
    "    model.maximize(\n",
    "        sum(\n",
    "            connections[g1][g2] * colocated[g1, g2]\n",
    "            for g1 in range(num_guests - 1)\n",
    "            for g2 in range(g1 + 1, num_guests)\n",
    "            if connections[g1][g2] > 0\n",
    "        )\n",
    "    )\n",
    "\n",
    "    #\n",
    "    # Constraints\n",
    "    #\n",
    "\n",
    "    # Everybody seats at one table.\n",
    "    for g in all_guests:\n",
    "        model.add(sum(seats[(t, g)] for t in all_tables) == 1)\n",
    "\n",
    "    # Tables have a max capacity.\n",
    "    for t in all_tables:\n",
    "        model.add(sum(seats[(t, g)] for g in all_guests) <= table_capacity)\n",
    "\n",
    "    # Link colocated with seats\n",
    "    for g1 in range(num_guests - 1):\n",
    "        for g2 in range(g1 + 1, num_guests):\n",
    "            for t in all_tables:\n",
    "                # Link same_table and seats.\n",
    "                model.add_bool_or(\n",
    "                    [\n",
    "                        ~seats[(t, g1)],\n",
    "                        ~seats[(t, g2)],\n",
    "                        same_table[(g1, g2, t)],\n",
    "                    ]\n",
    "                )\n",
    "                model.add_implication(same_table[(g1, g2, t)], seats[(t, g1)])\n",
    "                model.add_implication(same_table[(g1, g2, t)], seats[(t, g2)])\n",
    "\n",
    "            # Link colocated and same_table.\n",
    "            model.add(\n",
    "                sum(same_table[(g1, g2, t)] for t in all_tables) == colocated[(g1, g2)]\n",
    "            )\n",
    "\n",
    "    # Min known neighbors rule.\n",
    "    for g in all_guests:\n",
    "        model.add(\n",
    "            sum(\n",
    "                same_table[(g, g2, t)]\n",
    "                for g2 in range(g + 1, num_guests)\n",
    "                for t in all_tables\n",
    "                if connections[g][g2] > 0\n",
    "            )\n",
    "            + sum(\n",
    "                same_table[(g1, g, t)]\n",
    "                for g1 in range(g)\n",
    "                for t in all_tables\n",
    "                if connections[g1][g] > 0\n",
    "            )\n",
    "            >= min_known_neighbors\n",
    "        )\n",
    "\n",
    "    # Symmetry breaking. First guest seats on the first table.\n",
    "    model.add(seats[(0, 0)] == 1)\n",
    "\n",
    "    ### Solve model.\n",
    "    solver = cp_model.CpSolver()\n",
    "    solution_printer = WeddingChartPrinter(seats, names, num_tables, num_guests)\n",
    "    solver.solve(model, solution_printer)\n",
    "\n",
    "    print(\"Statistics\")\n",
    "    print(\"  - conflicts    : %i\" % solver.num_conflicts)\n",
    "    print(\"  - branches     : %i\" % solver.num_branches)\n",
    "    print(\"  - wall time    : %f s\" % solver.wall_time)\n",
    "    print(\"  - num solutions: %i\" % solution_printer.num_solutions())\n",
    "\n",
    "\n",
    "def main(argv: Sequence[str]) -> None:\n",
    "    if len(argv) > 1:\n",
    "        raise app.UsageError(\"Too many command-line arguments.\")\n",
    "    solve_with_discrete_model()\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
